package com.ruoyi.web.controller.mdm;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.mdm.domain.SkuMergeMasterSubImport;
import jakarta.servlet.http.HttpServletResponse;

import com.ruoyi.mdm.domain.SkuMasterSubImport;
import com.ruoyi.mdm.domain.SkuMergeMasterSubImport;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.mdm.domain.SkuMaster;
import com.ruoyi.mdm.service.ISkuMasterService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * sku管理Controller
 *
 * @author shiseido
 * @date 2025-03-01
 */
@RestController
@RequestMapping("/mdm/skuMaster")
public class SkuMasterController extends BaseController {
    @Autowired
    private ISkuMasterService skuMasterService;

    /**
     * 查询sku管理列表
     */
    @ApiOperation("查询sku管理列表")
    @GetMapping("/list")
    public TableDataInfo list(SkuMaster skuMaster) {
        startPage();
        List<SkuMaster> list = skuMasterService.selectSkuMasterList(skuMaster);
        return getDataTable(list);
    }

    /**
     * 导入产品属性
     */
    @ApiOperation("导入sku产品属性")
    @PreAuthorize("@ss.hasPermi('mdm:skuMaster:import')")
    @Log(title = "SKU管理", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception {

        ExcelUtil<SkuMasterSubImport> util1 = new ExcelUtil<SkuMasterSubImport>(SkuMasterSubImport.class);
        List<SkuMasterSubImport> skuMasterSubImportList = util1.importExcel(
                "渠道和推荐品", file.getInputStream(), 0);

//        ExcelUtil<SkuMergeMasterSubImport> util2 = new ExcelUtil<SkuMergeMasterSubImport>(SkuMergeMasterSubImport.class);
//        List<SkuMergeMasterSubImport> skuMergeMasterSubImportList = util2.importExcel(
//                "关联SKUM", file.getInputStream(), 0);
        List<SkuMergeMasterSubImport> skuMergeMasterSubImportList = new ArrayList<>();

//        ExcelUtil<SkuMergeMasterSubImport> util3 = new ExcelUtil<SkuMergeMasterSubImport>(SkuMergeMasterSubImport.class);
//        List<SkuMergeMasterSubImport> spuContentList = util3.importExcel(
//                "产品容量和关联SPU", file.getInputStream(), 0);
        List<SkuMergeMasterSubImport> spuContentList = new ArrayList<>();

        String operName = getUsername();

        skuMasterService.importSkuMasterSub(skuMasterSubImportList, spuContentList, operName);

        return success();
    }

    /**
     * 导出sku管理列表
     */
    @ApiOperation("导出sku管理列表")
    @PreAuthorize("@ss.hasPermi('mdm:skuMaster:export')")
    @Log(title = "SKU管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SkuMaster skuMaster) {
        List<SkuMaster> list = skuMasterService.selectSkuMasterList(skuMaster);
        ExcelUtil<SkuMaster> util = new ExcelUtil<SkuMaster>(SkuMaster.class);
        util.exportExcel(response, list, "产品管理数据");
    }

    /**
     * 获取sku管理详细信息
     */
    @ApiOperation("获取sku管理详细信息")
    @PreAuthorize("@ss.hasPermi('mdm:skuMaster:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(skuMasterService.selectSkuMasterById(id));
    }

    /**
     * 新增sku管理
     */
    @ApiOperation("新增sku管理")
    @PreAuthorize("@ss.hasPermi('mdm:skuMaster:add')")
    @Log(title = "sku管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SkuMaster skuMaster) {
        skuMaster.setCreateBy(getUsername());
        return toAjax(skuMasterService.insertSkuMaster(skuMaster));
    }

    /**
     * 修改sku管理
     */
    @ApiOperation("修改sku管理")
    @PreAuthorize("@ss.hasPermi('mdm:skuMaster:edit')")
    @Log(title = "sku管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SkuMaster skuMaster) {
        skuMaster.setCreateBy(getUsername());
        skuMaster.setUpdateBy(getUsername());
        return toAjax(skuMasterService.updateSkuMaster(skuMaster));
    }

    /**
     * 删除sku管理
     */
    @ApiOperation("删除sku管理")
    @PreAuthorize("@ss.hasPermi('mdm:skuMaster:remove')")
    @Log(title = "sku管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(skuMasterService.deleteSkuMasterByIds(ids));
    }

    /**
     * 查询SKU管理列表
     */
    @ApiOperation("通过SKUM编码查询sku信息列表")
    @GetMapping("/getSkuBySkumCode/{skumCode}")
    public TableDataInfo getSkuBySkumCode(@PathVariable("skumCode") String skumCode) {
        startPage();
        List<SkuMaster> list = skuMasterService.selectSkuMasterBySkumCode(skumCode);
        return getDataTable(list);
    }

    /**
     * 查询SKU管理列表
     */
    @ApiOperation("通过SPU编码查询sku信息列表")
    @GetMapping("/getSkuBySpuCode/{spuCode}")
    public TableDataInfo getSkuBySpuCode(@PathVariable("spuCode") String spuCode) {
        startPage();
        List<SkuMaster> list = skuMasterService.selectSkuMasterBySpuCode(spuCode);
        return getDataTable(list);
    }

}
